<%@page import="entities.Website"%>
<%@page import="entities.Webmail"%>
<%@page import="helpers.CookieHelper"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.google.appengine.api.datastore.Category"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="helpers.PaginatorHelper"%>
<%
List<Webmail> webmails = (List<Webmail>) request.getAttribute("webmails");
Map<String, Website> websites = (Map<String, Website>) request.getAttribute("websites");
Map<String, Category> categories = (Map<String, Category>) request.getAttribute("categories"); 
int pages = (Integer) request.getAttribute("page");
boolean nextPage = (Boolean) request.getAttribute("nextPage");
boolean prevPage = (Boolean) request.getAttribute("prevPage");
PaginatorHelper paginator = (PaginatorHelper) request.getAttribute("paginator");
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/layouts/header.jsp" />

<script type="text/javascript" src="/js/website_list.js"></script>

	<h3>List Webmail</h3>

	<% if (CookieHelper.getCookie(request, "success") != null && !CookieHelper.getCookie(request, "success").isEmpty()) {%>
	<div class="notification msgsuccess">
        <a class="close"></a>
        <p><%=CookieHelper.getCookie(request, "success") %></p>
        <%
        //Remove cookie
        CookieHelper.setCookie(response, "success", "");
        %>
    </div>
	<% } %>

	<div class="tableoptions">
       <select class="span4 action-value">
       	<option value="status_active">Change to Status Active</option>
           <option value="status_inactive">Change to Status Inactive</option>
           <option value="delete">Delete all selected</option>
       </select> &nbsp;
       <button class="btn apply-change">Apply</button>
   </div>
   
   <table class="table" id="list-table">
        <thead>
            <tr>
            	<th><span class="checkbox"><input type="checkbox" class="checkall"></span></th>
                <th>Id</th>
                <th>Website</th>
                <th>Website Title</th>
                <th>Total Email</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        	<% 
        	for (int i = 0; i < webmails.size(); i++) { 
        		Website website = websites.get(webmails.get(i).getId());
        		if (website != null) {
        	%>
            <tr>
            	<td><span class="checkbox"><input type="checkbox" value="<%=URLEncoder.encode(webmails.get(i).getId())%>"></span></td>
                <td><%=webmails.get(i).getId() %></td>
                <td><%=website.getDomain() %></td>
                <td><%=website.getTitle() %></td>
                <td><%=webmails.get(i).getCount() %></td>
                <td class="center">
                	<%
					switch (webmails.get(i).getStatus()) {
						case 0: 
							%>Not Send<%
						break;
						case 1:
							%>Wating<%
							break;
						case 2:
							%>Sending<%
							break;
						case 3:
							%>Finished<%
							break;
						default:
							break;
					}
					%>
                </td>
                <td>
                	<a title="Send Webmail" href="/webmail/send/<%=URLEncoder.encode(webmails.get(i).getId())%>" class="send">
						Send mail
					</a>&nbsp;
					<a title="Remove Webmail" href="/webmail/delete/<%=URLEncoder.encode(webmails.get(i).getId())%>" id="action_del" class="delete">
						Delete
					</a>
				</td>
             </tr>
             <%	 } 
             } %>
        </tbody>
    </table>
   
   
   <div class="pagination">
		<%
		List<Integer> range = paginator.getRangePage();
		%>
                 <% if (prevPage) { %>
		<a href="<%=paginator.getPrev()%>" class="prev page-numbers">Prev</a>
		<% } %>
		<%
		if (nextPage || prevPage) {
			for (int i = 0; i < range.size(); i++) {
				if (range.get(i).intValue() == pages) {
		%>
		<span class="page-numbers current"> <%=range.get(i).toString() %></span>
			<%  } else { %>
		<a class="page-numbers" href="<%=paginator.getPage(range.get(i).intValue())%>"> <%=range.get(i).toString() %></a>
		<%  	}
			}
		} %>
		
		<% if (nextPage) { %>
		<a href="<%=paginator.getNext()%>" class="next page-numbers">Next</a></li>
		<% } %>
	</div>
   		
<jsp:include page="/layouts/footer.jsp" />